package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysLab;
import com.ruoyi.system.mapper.SysArtistLabMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysLabMapper;
import com.ruoyi.system.service.ISysLabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 标签管理Service业务层处理
 * 
 * @author cooc
 * @date 2024-06-16
 */
@Service
public class SysLabServiceImpl implements ISysLabService 
{
    @Autowired
    private SysLabMapper sysLabMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private SysArtistLabMapper sysArtistLabMapper;

    /**
     * 查询标签管理
     * 
     * @param id 标签管理主键
     * @return 标签管理
     */
    @Override
    public SysLab selectSysLabById(Long id)
    {
        return sysLabMapper.selectSysLabById(id);
    }

    /**
     * 查询标签管理列表
     * 
     * @param sysLab 标签管理
     * @return 标签管理
     */
    @Override
    public List<SysLab> selectSysLabList(SysLab sysLab)
    {
        return sysLabMapper.selectSysLabList(sysLab);
    }

    /**
     * 新增标签管理
     * 
     * @param sysLab 标签管理
     * @return 结果
     */
    @Override
    public int insertSysLab(SysLab sysLab)
    {
        return sysLabMapper.insertSysLab(sysLab);
    }

    /**
     * 修改标签管理
     * 
     * @param sysLab 标签管理
     * @return 结果
     */
    @Override
    public int updateSysLab(SysLab sysLab)
    {
        return sysLabMapper.updateSysLab(sysLab);
    }

    /**
     * 批量删除标签管理
     * 
     * @param ids 需要删除的标签管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysLabByIds(Long[] ids)
    {
        delArtistLabIds(ids);
        return sysLabMapper.deleteSysLabByIds(ids);
    }

    /**
     * 删除标签管理信息
     * 
     * @param id 标签管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysLabById(Long id)
    {
        delArtistLabs(id);
        return sysLabMapper.deleteSysLabById(id);
    }

    public List<SysLab> getLabOptions (String groupKeyName) {
        return sysLabMapper.selectSysLabList(new SysLab());
    }

    @Override
    public boolean checkNameUnique(SysLab sysLab) {
        Long labId = StringUtils.isNull(sysLab.getId()) ? -1L : sysLab.getId();
        SysLab info = sysLabMapper.checkNameUnique(sysLab.getName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != labId.longValue()) {
            return false;
        }
        return true;
    }

    private void delArtistLabs(Long labId){
        sysArtistLabMapper.deleteSysArtistLabByLabId(labId);
    }

    private void delArtistLabIds(Long[] labIds){
        sysArtistLabMapper.deleteSysArtistLabByLabIds(labIds);
    }
}
